package top.lenjon.lib.mapper;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import top.lenjon.lib.entity.Book;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.lenjon.lib.entity.BookClassify;

import java.util.List;

/**
 * <p>
 * 附件 Mapper 接口
 * </p>
 *
 * @author wei
 * @since 2020-11-09
 */
@Repository
public interface BookMapper extends BaseMapper<Book> {

    String LIMIT = " LIMIT #{pageIndex},#{pageNum}";

    /**
     * 分页查找书本，pageNum为null则不分页
     *
     * @param pageIndex
     * @param pageNum
     * @return
     */
    @Select({"<script>",
            "SELECT * FROM book",
            "<when test='pageNum!=null'>",
            LIMIT,
            "</when>",
            "</script>"})
    List<Book> getBooks(Integer pageIndex, Integer pageNum);

    /**
     * 按数组取id
     *
     * @param list 整型数组
     * @return
     */
    @Deprecated
    @Select({"<script>",
            "SELECT * FROM book",
            "<if test='list != null and list.length > 0'>",
            "WHERE book_id IN",
            "<foreach collection='list' index='index' item='item'>",
            "open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</if>",
            "</script>"
    })
    List<Book> getBooksByIntArray(List<Integer> list);

    /**
     * 模糊查找书名
     *
     * @param title
     * @return
     */
    @Select("SELECT * FROM book WHERE title LIKE '%${title}%'")
    List<Book> getBooksByTitle(String title);

    @Select({"<script>",
            "SELECT book.* FROM book INNER JOIN book_classify bc ON book.id = bc.book_id WHERE 1=1",
            "<if test='zhengZhi!=null'>AND zheng_zhi=1</if>",
            "<if test='jingJi!=null'>AND jing_ji=1</if>",
            "<if test='wenHua!=null'>AND wen_hua=1</if>",
            "<if test='yuLe!=null'>AND yu_le=1</if>",
            "<if test='keXue!=null'>AND ke_xue=1</if>",
            "<if test='gongYe!=null'>AND gong_ye=1</if>",
            "<if test='zongHe!=null'>AND zong_he=1</if>",
            "</script>"
    })
    List<Book> getAllBookByClassify(BookClassify classify);

    @Update("UPDATE book SET status = #{status} WHERE id=#{id}")
    int setStatusById(int id,boolean status);
}
